package rai.form;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import rai.util.Constants;

/**
 * EditEmpForm.
 * @author Tien
 *
 */
public class EditEmpInfoForm {
    /** Employee Information property. */
    private String txtEmpName;
    /** Employee Birthday. */
    private String txtBirthday;
    /** Employee Address. */
    private String txtAddress;
    /** Employee Email. */
    private String txtEmail;
    /** Employee Phone. */
    private String txtPhone;

    /**
     * Get Name.
     * @return Name
     */
    public String getName() {
        return txtEmpName;
    }

    /**
     * Get Birthday.
     * @return date
     */
    public String getBirthday() {
        return txtBirthday;
    }

    /**
     * Get Add.
     * @return address
     */
    public String getAddress() {
        return txtAddress;
    }

    /**
     * Get Email.
     * @return email
     */
    public String getEmail() {
        return txtEmail;
    }

    /**
     * Get Phone.
     * @return phone
     */
    public String getPhone() {
        return txtPhone;
    }

   /**
    * set Name.
    * @param Name
    */
    public void setName(final String Name) {
        this.txtEmpName = Name;
    }

    /**
     * @param Birthday
     *            the Employee Birthday to set
     */
    public void setBirthday(final String Birthday) {
        this.txtBirthday = Birthday;
    }

    /**
     * @param Address
     *            the Employee Address to set
     */
    public void setAddress(final String Address) {
        this.txtAddress = Address;
    }

    /**
     * @param Email
     *            the Employee Email to set
     */
    public void setEmail(final String Email) {
        this.txtEmail = Email;
    }

    /**
     * @param Phone
     *            the Employee Phone Number to set
     */
    public void setPhone(final String Phone) {
        this.txtPhone = Phone;
    }
    /** log property. */
    private static Logger log = Logger.getLogger("EditEmpInfoForm");

    /**
     * check Email.
     * @param email
     * @return String
     */
    public String checkEmail(final String email) {
        final String error = null;
        // check if '@' is at the first position or at last position or absent
        // in given email
        if (email.charAt(0) == '@' || email.charAt(email.length() - 1) == '@'
                || email.indexOf("@") == -1) {
            return "Invalid Email Address1";
        }
        // check if '.' is at the first position or at last position or absent
        // in given email
        if (email.charAt(0) == '.' || email.charAt(email.length() - 1) == '.'
                || email.indexOf(".") == email.length()) {
            return "Invalid Email Address2";
        }
        // check if '@' is used more than one times in given email
        if (email.indexOf("@", email.indexOf("@") + 1) != -1) {
            return "Invalid Email Address3";
        }
        // check for the position of '.'
        if (email.indexOf(".") - email.indexOf("@") <= 2) {
            return "Invalid Email Address4";
        }
        // check for blank spaces in given email
        if (email.indexOf(" ") != -1) {
            return "Invalid Email Address6";
        }
        return error;
    }

    /**
     * Check Name.
     * @param string
     * @return boolean
     */
    public boolean checkName(final String string) {
        log.debug("checkName.start");
        boolean check = true;
        if (string.charAt(0) == ' '
                || string.charAt(string.length() - 1) == ' ') {

             check = false;
        } else {
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
                    continue;
                } else if (string.charAt(i) >= 'a' && string.charAt(i) <= 'z') {
                    continue;
                } else if (string.charAt(i) == ' ' && string.charAt(i + 1) != ' ') {
                    continue;
                } else {
                    check = false;
                    break;
                }
            }
        }
        log.debug("checkName.end." + check);
        return check;
    }

    /**
     * Check Address.
     * @param string
     * @return boolean
     */
    public boolean checkAddress(final String string) {
        boolean check = true;
        if (string.charAt(0) == ' '
                || string.charAt(string.length() - 1) == ' ') {
            check = false;
        } else {
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) >= '0' && string.charAt(i) <= '9') {
                    continue;
                } else if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
                    continue;
                } else if (string.charAt(i) >= 'a' && string.charAt(i) <= 'z') {
                    continue;
                } else if (string.charAt(i) == '/' || string.charAt(i) == '-'
                        || string.charAt(i) == '.') {
                    continue;
                } else if (string.charAt(i) == ' ' && string.charAt(i + 1) != ' ') {
                    continue;
                } else {
                    check = false;
                    break;
                }
            }
        }
        log.debug("checkAddress.end." + check);
        return check;
    }

    /**
     * Check Phone.
     * @param phone
     * @return
     */
    public boolean checkPhone(final String phone) {
        for (int i = 0; i < phone.length(); i++) {
            if (phone.charAt(i) == '(') {
                continue;
            } else if (phone.charAt(i) >= '0' && phone.charAt(i) <= '9') {
                continue;
            } else if (phone.charAt(i) != ')') {
                continue;
            } else if (phone.charAt(i) != '.') {
                continue;
            } else {
                return false;
            }
        }
        if (phone.length() == 12) {
            if (phone.charAt(3) != '.') {
                return false;
            } else if (phone.charAt(7) != '.')
                return false;
        } else if (phone.length() == 13) {
            if (phone.charAt(4) != '.') {
                return false;
            } else if (phone.charAt(8) != '.') {
                return false;
            }
        } else {
            return false;
        }
        log.debug("checkPhone.end.");
        return true;
    }
    /**
     * Validate EmpInfo.
     * @param request
     * @param response
     * @return
     */
    public List<String> validateEditEmpInfo(final RenderRequest request,
            final RenderResponse response) {
        log.debug("validateEditEmpInfo.start");
        final List<String> errorMess = new ArrayList<String>();
        // check Employee Name
        if (this.txtEmpName.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input Employee Name!");
            log.debug("validate.errorMess: Please input Emp Name!");
        } else if (this.txtEmpName.length() < 3) {
            errorMess.add("Name must has at least 3 letters!");
            log.debug("validate.errorMess:Name must has at least 3 letters!");
        } else if (!checkName(txtEmpName)) {
            errorMess
                    .add("Name only contains letters, not start with a blank and every part seperated by a blank!");
            log
                    .debug("validate.errorMess:Name only contains letters, not start with a blank and every part "
                            + "seperated by a blank!");
        }
        request.getPortletSession().setAttribute("editEmpName", txtEmpName,
                PortletSession.APPLICATION_SCOPE);
        // check Address
        if (this.txtAddress.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Address!");
            log.debug("validate.errorMess: Please input the Address!");
        } else if (this.txtAddress.length() < 10) {
            errorMess.add("Address must has at least 10 letters!");
            log
                    .debug("validate.errorMess:Address must has at least 10 letters!");
        } else if (!checkAddress(txtAddress)) {
            errorMess
                    .add("Address only contains letters and numbers,not start with a blank and separated by a blank!");
            log
                    .debug("validate.errorMess:Address only contains letters and numbers,not start with a blank "
                            + "and separated by a blank!");
        }
        request.getPortletSession().setAttribute("editAddress", txtAddress,
                PortletSession.APPLICATION_SCOPE);
        // check Email
        if (this.txtEmail.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Email Address!");
            log.debug("validate.errorMess:Please input the Email Address!");
        } else if (checkEmail(txtEmail) != null) {
            errorMess.add(checkEmail(txtEmail));
            log.debug("validate.errorMess:" + checkEmail(txtEmail));
        }
        request.getPortletSession().setAttribute("editEmail", txtEmail,
                PortletSession.APPLICATION_SCOPE);
        // check Phone
        if (this.txtPhone.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input Phone Number!");
            log.debug("validate.errorMess: Please input Phone Number!");
        } else if (!checkPhone(txtPhone)) {
            errorMess
                    .add("Invalid Phone Number. Phone has format such as: 0123.456.7890!");
            log
                    .debug("validate.errorMess: Invalid Phone Number.Phone has format such as: 0123.456.7890!");
        }
        log.debug("validateEditEmpInfo.end");
        return errorMess;
    }
}
